home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992…tember: A ROM With a View / devSep92 / devSep92.dmg / Tools & Apps / Testing & Debugging / Report Error 1.2 / reportError.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-31  |  4.5 KB  |  195 lines  |  [TEXT/KAHL]

  1. /*================================================================================
  2.     reportError.c
  3.     version 1.1
  4.     
  5.     Greg Anderson
  6.     28 June 1991
  7.     greggor@apple.com
  8.     
  9.     These routines are very convenient for debugging; they will convert
  10.     OSErrors from short negative numbers to human-readable strings in
  11.     Pascal format
  12.     
  13.     This code depends on:
  14.     
  15.         stringUtilities.c
  16.         dialogUtilities.c
  17.         
  18.         'DLOG' resource ID 30303    A simple dialog box for displaying errors;
  19.         'DITL' resource ID 30303    Has one 'Okay' button and one static text
  20.                                     item that reads:
  21.                                     
  22.                                     ^0 ^1
  23.                                     
  24.                                     ^2 because ^3
  25.                                     
  26.         'TEXT' resource ID 30303    All error messages, each line cr-terminated
  27.         
  28. ================================================================================*/
  29. #include "reportError.h"
  30.  
  31. /*
  32. // Header files for utility routines:
  33. */
  34. #ifndef __DIALOGUTILITIES__
  35.     #include "dialogUtilities.h"
  36. #endif
  37. #ifndef __STRINGUTILITIES__
  38.     #include "stringUtilities.h"
  39. #endif
  40.  
  41. /*
  42. // Macintosh includes
  43. */
  44. #ifndef __TYPES__
  45.     #include <Types.h>
  46. #endif
  47. #ifndef __ERRORS__
  48.     #include <Errors.h>
  49. #endif
  50. #ifndef __MEMORY__
  51.     #include <Memory.h>
  52. #endif
  53. #ifndef __RESOURCES__
  54.     #include <Resources.h>
  55. #endif
  56. #ifndef __PACKAGES__
  57.     #include <Packages.h>
  58. #endif
  59.  
  60.  
  61. Handle        resultCodeText = nil;
  62.  
  63. /*--------------------------------------------------------------------------------
  64.     Search for an error result code in the result code text
  65.     
  66.     Return 'false' if the name and description strings cannot be filled in
  67. --------------------------------------------------------------------------------*/
  68. pascal Boolean FindResultCodeDescription( OSErr theErr, Str255 errorNameStr, Str255 errorDescStr )
  69. {
  70.     Size        textSize;
  71.     char*        p;
  72.     Boolean        foundEntry;
  73.     OSErr        testError;
  74.     
  75.     /*
  76.     // Note that if the error is a 'memFullErr', it isn't going to do
  77.     // much good to try to load in the result code test.  Handle this
  78.     // case separately.
  79.     //
  80.     // While there are other situations that could also cause this
  81.     // routine to fail, none are as common as memFullErr.
  82.     */
  83.     if( theErr == memFullErr )
  84.     {
  85.         CtoPcpy( errorNameStr, "memFullErr" );
  86.         CtoPcpy( errorDescStr, "there is insufficient memory left in the heap zone" );
  87.         
  88.         return true;
  89.     }
  90.     /*
  91.     // If we have never loaded the result code text, do so now
  92.     */
  93.     if( resultCodeText == nil )
  94.         resultCodeText = GetResource( 'TEXT', kReportErrorID );
  95.     if( resultCodeText == nil )
  96.         return false;
  97.     /*
  98.     // If the result code text has been purged, try to re-load it.
  99.     */
  100.     if( *resultCodeText == nil )
  101.         LoadResource( resultCodeText );
  102.     if( *resultCodeText == nil )
  103.         return false;
  104.     /*
  105.     // Lock the result text & dereference it
  106.     */
  107.     MoveHHi( resultCodeText );
  108.     HLock( resultCodeText );
  109.     p = *resultCodeText;
  110.     textSize = GetHandleSize( resultCodeText );
  111.     foundEntry = false;
  112.     do
  113.     {
  114.         SkipToSpec( p, '-' );
  115.         if( !(*p) )
  116.             break;
  117.         ++p;
  118.         
  119.         testError = -ScanNumberInString( &p );
  120.         if( testError == theErr )
  121.         {
  122.             SkipPastWhitespace( p );
  123.             ScanWordInString( &p, errorNameStr );
  124.             SkipPastWhitespace( p );
  125.             ScanLineInString( &p, errorDescStr );
  126.             foundEntry = true;
  127.             break;
  128.         }
  129.     }
  130.     while( testError != -32767 );
  131.     /*
  132.     // Unlock the result text and return
  133.     */
  134.     HUnlock( resultCodeText );
  135.     return foundEntry;
  136. }
  137.  
  138. /*--------------------------------------------------------------------------------
  139.     Put up a dialog box reporting an error to the user
  140. --------------------------------------------------------------------------------*/
  141. pascal void ReportError( Str255 errorMsg, OSErr theErr )
  142. {
  143.     DialogPtr        dlog;
  144.     short            itemHit;
  145.     Str255            errorNumberStr;
  146.     Str255            errorNameStr;
  147.     Str255            errorDescStr;
  148.     long            longErr = theErr;
  149.     
  150.     /*
  151.     // Do nothing if theErr == noErr
  152.     */
  153.     if( theErr != noErr )
  154.     {
  155.         /*
  156.         // Clear out the pascal strings, just to be safe
  157.         */
  158.         errorNameStr[0] = 0;
  159.         errorDescStr[0] = 0;
  160.         errorNumberStr[0] = 0;
  161.         /*
  162.         // Set up the error number string
  163.         */
  164.         NumToString( longErr, errorNumberStr );
  165.         FindResultCodeDescription( theErr, errorNameStr, errorDescStr );
  166.         if( errorDescStr[0] == 0 )
  167.             CtoPcpy( errorDescStr, "an error ocurred" );
  168.         /*
  169.         // Set up Dialog box
  170.         */
  171.         dlog = GetNewDialog(kReportErrorID, (Ptr)0L, (WindowPtr)-1L);
  172.         if( dlog != nil )
  173.         {
  174.             SetPort( dlog );
  175.             ParamText( errorMsg, errorNumberStr, errorNameStr, errorDescStr );
  176.             SelectWindow(dlog);
  177.             InstallDefaultOutline( dlog, 1 );
  178.             CenterAndShowDialog(dlog);
  179.                         
  180.             /*
  181.             // Wait for user to click "Okay"
  182.             */
  183.             do
  184.             {
  185.                 ModalDialog((ModalFilterProcPtr) CutPasteFilter, &itemHit);
  186.             } while( itemHit > 2 );
  187.             DisposDialog(dlog);
  188.         }
  189.         else
  190.         {
  191.             DebugStr( (unsigned char*)"\pReportError could not bring up dialog box" );
  192.         }
  193.     }
  194. }
  195.